﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain
{
	using System;
	using System.Collections.Generic;
	using Falcon.Core.Domain.Enums;
	using Falcon.Core.Extensions;

	public class UserMessageGrouped
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="UserMessageGrouped"/> class.
		/// </summary>
		/// <param name="filter">The filter.</param>
		/// <param name="messages">The messages.</param>
		public UserMessageGrouped(UserMessageGroupedFilter filter, ICollection<UserMessage> messages)
		{
			var date = DateTime.Now;

			this.Messages = new List<UserMessage>();

			switch (filter)
			{
				case UserMessageGroupedFilter.Today:
					AddToday(date, messages);
					break;
				case UserMessageGroupedFilter.Yesterday:
					AddYesterday(date, messages);
					break;
				case UserMessageGroupedFilter.ThisWeek:
					AddThisWeek(date, messages);
					break;
				default:
					AddOlder(date, messages);
					break;
			}

			this.Slug = this.Title.Slug();

			foreach (var message in Messages)
			{
				messages.Remove(message);
			}
		}

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public string Title
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the slug.
		/// </summary>
		/// <value>The slug.</value>
		public string Slug
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the messages.
		/// </summary>
		/// <value>The messages.</value>
		public IList<UserMessage> Messages
		{
			get;
			private set;
		}

		/// <summary>
		/// Adds the messages that are from today.
		/// </summary>
		/// <param name="date">The date.</param>
		/// <param name="messages">The messages.</param>
		private void AddToday(DateTime date, IEnumerable<UserMessage> messages)
		{
			this.Title = Resources.Titles.Today;

			foreach (var message in messages)
			{
				if (message.Created.Month == date.Month && 
					message.Created.Day == date.Day && 
					message.Created.Year == date.Year)
				{
					Messages.Add(message);
				}
			}
		}

		/// <summary>
		/// Adds the messages that are from yesterday.
		/// </summary>
		/// <param name="date">The date.</param>
		/// <param name="messages">The messages.</param>
		private void AddYesterday(DateTime date, IEnumerable<UserMessage> messages)
		{
			this.Title = Resources.Titles.Yesterday;

			foreach (var message in messages)
			{
				if (message.Created.Month == date.Month && 
					message.Created.Day == date.AddDays(-1).Day && 
					message.Created.Year == date.Year)
				{
					Messages.Add(message);
				}
			}
		}

		/// <summary>
		/// Adds the messages that are from this week.
		/// </summary>
		/// <param name="date">The date.</param>
		/// <param name="messages">The messages.</param>
		private void AddThisWeek(DateTime date, IEnumerable<UserMessage> messages)
		{
			this.Title = Resources.Titles.ThisWeek;

			foreach (var message in messages)
			{
				if (message.Created.Month == date.Month && 
					message.Created.Day < date.AddDays(-1).Day && 
					message.Created.Day > date.AddDays(-7).Day && 
					message.Created.Year == date.Year)
				{
					Messages.Add(message);
				}
			}
		}

		/// <summary>
		/// Adds the messages that are older then today, yesterday or this week
		/// </summary>
		/// <param name="date">The date.</param>
		/// <param name="messages">The messages.</param>
		private void AddOlder(DateTime date, IEnumerable<UserMessage> messages)
		{
			this.Title = Resources.Titles.Older;

			foreach (var message in messages)
			{
				if (message.Created.Day <= date.AddDays(-7).Day)
				{
					Messages.Add(message);
				}
			}
		}
	}
}